﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using DnEcuDiag.Devices.Enums;
using DnEcuDiag.Devices.Interfaces;
using DnEcuDiag.Devices.Responses;

namespace DnEcuDiag.Tests
{
    public class IsoTestDevice : IDevice
    {
        public List<Byte[]> Faultcodeinfo;
        public byte[] FaultcodeList;
        public List<Byte[]> SensorData;
        public byte[] SnapShotData;

        public byte KB2
        {
            get { return 0; }
        }

        public byte KB1
        {
            get { return 0; }
        }

        #region IDevice Members

        public Response SendCommandWithInit(byte ecuId, byte testerId, byte[] commandToSend, DeviceInitType type)
        {
            if (commandToSend.Length == 1)
            {
                if (commandToSend[0] == 0x9)
                    return new KwpResponse(new List<byte[]> { new byte[] { 2, 0x0A, 0x0C } });
            }

            return null;
        }

        public int EcuResponseTimeout
        {
            get { return 0; }
            set { }
        }

        public int EcuResponseInterbyteTimeout
        {
            get { return 0; }
            set { }
        }

        public void Close()
        {
        }

        public byte[] KeyBytes
        {
            get { return new byte[] { 0x55, 0xA7, 0x86, 0x85, 0x97, 0x9E, 0x79 }; }
        }

        public Response SendCommand(byte ecuId, byte testerId, byte[] commandToSend)
        {
            if (commandToSend.Length == 1)
            {
                if (commandToSend[0] == 0x9)
                    return new KwpResponse(new List<byte[]> { new byte[] { 2, 0x0A, 0x0C } });

                if (commandToSend[0] == 0)
                    return
                        new KwpResponse(new List<byte[]> {new byte[]
                                            {
                                                0x0E, 0xF6, 0x01, 0x01, 0x98, 0x33, 0x10, 0x07, 0x11, 0x00, 0x60, 0x65,
                                                0x80, 0x09, 0x47
                                            }});

                // Read fault code.
                if (commandToSend[0] == 7)
                    return new KwpResponse(Faultcodeinfo);
            }

            if (commandToSend.Length == 2)
            {
                if (commandToSend[0] == 0x12)
                {
                    return new KwpResponse(new List<byte[]> { SnapShotData });
                }

                if (commandToSend[0] == 0x08)
                {
                    return new KwpResponse(SensorData);
                }
            }

            if (commandToSend.Length == 4)
            {
                if (commandToSend[0] == 0x1)
                {
                    return new KwpResponse(SensorData);
                }
            }

            if (commandToSend[0] == 2)
            {
                return new KwpResponse(new List<byte[]> { new byte[] { 0x9, 0x9, 0x09 } });
            }

            if (commandToSend[0] == 0x27)
            {
                return new KwpResponse(new List<byte[]> { new byte[] { 0xE8, 0xE8, 0xE8 } });
            }
            return null;
        }

        public int RequestInterByteTime
        {
            get { return 0; }
            set { }
        }

        public bool EnableTrace
        {
            get { throw new Exception("The method or operation is not implemented."); }
            set { throw new Exception("The method or operation is not implemented."); }
        }

        public ReadOnlyCollection<byte[]> IdentificationData { get; set; }

        public void Dispose()
        {
        }

        #endregion
    }
}
